package com.nanyang.tms.controller.bill;

import javax.validation.Valid;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.nanyang.common.core.annotation.BindParam;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ServiceException;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.tms.api.dto.request.bill.BillAddRequest;
import com.nanyang.tms.api.dto.request.bill.BillQueryRequest;
import com.nanyang.tms.api.dto.request.bill.BillRecordQueryRequest;
import com.nanyang.tms.api.dto.request.bill.BillRecordUpdateRequest;
import com.nanyang.tms.api.dto.response.bill.BillPageResponse;
import com.nanyang.tms.api.dto.response.bill.BillRecordPageResponse;
import com.nanyang.tms.api.service.bill.BillRpcService;

@RestController
@RequestMapping("/bill")
public class BillController {
    @Reference(version = "1.0.0", timeout = 10000, check = false)
    private BillRpcService billRpcService;

    /**
     * <p>listBill</p>
     * <p>Description:查询表</p>
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResult<PageBean<BillPageResponse>> listBill(@RequestBody BillQueryRequest param) {
        ApiResult<PageBean<BillPageResponse>> apiResult = billRpcService.listBill(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * <p>listBillRecord</p>
     * <p>Description:查询表</p>
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/listBillRecord", method = RequestMethod.POST)
    public ApiResult<PageBean<BillRecordPageResponse>> listBillRecord(@RequestBody @Valid BillRecordQueryRequest param) {
        ApiResult<PageBean<BillRecordPageResponse>> apiResult = billRpcService.listBillRecord(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 添加
     * @param param
     * @return
     */
    @BindParam
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ApiResult<Boolean> addBill(@BindParam @RequestBody @Valid BillAddRequest param) {
        ApiResult<Boolean> apiResult = billRpcService.addBill(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 对账
     * @param param
     * @return
     */
    @BindParam
    @RequestMapping(value = "/checkBill", method = RequestMethod.POST)
    public ApiResult<Boolean> checkBill(@BindParam @RequestBody @Valid BillRecordUpdateRequest param) {
        ApiResult<Boolean> apiResult = billRpcService.checkBill(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 处理差异
     * @param param
     * @return
     */
    @BindParam
    @RequestMapping(value = "/solveBill", method = RequestMethod.POST)
    public ApiResult<Boolean> solveBill(@BindParam @RequestBody @Valid BillRecordUpdateRequest param) {
        ApiResult<Boolean> apiResult = billRpcService.solveBill(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
}
